package com.manong.bookstore.user.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.manong.bookstore.user.entity.AddressEntity;
import com.manong.bookstore.user.service.AddressService;
import com.manong.common.utils.PageUtils;
import com.manong.common.utils.R;



/**
 * 
 *
 * @author cyq
 * @email 465447222@qq.com
 * @date 2021-01-27 21:34:32
 */
@RestController
@RequestMapping("user/address")
public class AddressController {
    @Autowired
    private AddressService addressService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = addressService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{addressId}")
    public R info(@PathVariable("addressId") Integer addressId){
		AddressEntity address = addressService.getById(addressId);

        return R.ok().put("address", address);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AddressEntity address){
		addressService.save(address);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AddressEntity address){
		addressService.updateById(address);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] addressIds){
		addressService.removeByIds(Arrays.asList(addressIds));

        return R.ok();
    }

    /**
     * 查询某个用户的所有收货地址
     * userId
     */
    @RequestMapping("/findUserAddress")
    public R findUserAddress(@RequestParam Map<String, Object> params) {
        String userId = (String)params.get("userId");

        if(userId == null)
            return R.ok().put("status", "error: miss parameter");

        List<AddressEntity> result = addressService.list(new QueryWrapper<AddressEntity>().eq("user_id", userId));
        
        return R.ok().put("status", "ok").put("address", result);
    }

    /**
     * 设置默认收货地址
     * userId, addressId
     */
    @RequestMapping("/setDefaultAddress")
    public R setDefaultAddress(@RequestParam Map<String, Object> params) {
        String userId = (String)params.get("userId");
        String addressId = (String)params.get("addressId");

        if(userId == null || addressId == null)
            return R.ok().put("status", "error: miss parameter");

        boolean status = addressService.setDefaultAddress(userId, addressId);

        return R.ok().put("status", status ? "ok" : "error: unknow");
    }
}
